<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card class="treeBox">
        <el-row>
            <el-col>
                <el-button type="primary" @click="showPopUpBox('添加分类')">添加分类</el-button>
            </el-col>
        </el-row>
        <!-- 表格区域 -->
        <tree-table
        class="treeBox"
        show-index
        border
        :data="careList"
        :columns='columns'
        :selection-type="false"
        :expand-type="false">
        <!-- 是否有效 -->
          <template slot="isok" slot-scope ='scope'>
              <i class="el-icon-success" style="color:lightgreen" v-if="scope.row.cat_deleted === false"></i>
              <i class="el-icon-error" style="color:red" v-else></i>
          </template>
          <template slot="order" slot-scope ='scope'>
            <el-tag size="mini" v-if="scope.row.cat_level ===0">一级</el-tag>
            <el-tag size="mini" type="success" v-else-if="scope.row.cat_level ===1">二级</el-tag>
            <el-tag size="mini" type="warning" v-else>三级</el-tag>
          </template>
          <template slot="opt" >
            <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
          </template>
        </tree-table>
        <!-- 分页区 -->
        <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[3, 5, 10, 15]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
    <!-- 弹出框 -->
      <PopUpBox v-if="isShow"
      ref="PopUpBoxRef"
      :tielename = tielename
      :addCase = addCase
      @change="setIsShow"
      >
          <!-- from表单 -->
          <!-- :rules="addCateFromRules"  -->
        <el-form :model="addCateFrom" ref="addCateFromRef" label-width="100px">
            <el-form-item label="分类名称">
                <el-input v-model="addCateFrom.cat_name"></el-input>
            </el-form-item>
        </el-form>
        <el-form :model="addCateFrom" ref="addCateFromRef" label-width="100px">
            <el-form-item label="父级分类">
              <el-cascader
                  v-model="selectedKeys"
                  :options="parentList"
                  :props="cascaderProps"
                  @change="parentCateChange"
                  clearable
                  change-on-select>
              </el-cascader>
            </el-form-item>
       </el-form>
      </PopUpBox>
  </div>
</template>

<script>
import PopUpBox from '@/views/PopUpBox '
import { instance } from '@/utils/http'
export default {
  components: {
    PopUpBox
  },
  created () {
    this.getcareList()
  },
  data () {
    return {
      // 对话框的属性------------
      isShow: false,
      tielename: '',
      addCateFrom: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      parentList: [],
      selectedKeys: [],
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'

      },
      // 对话框的属性------------

      careList: [],
      total: 0,
      //   查询条件
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      //   表格的配置数据
      columns: [{
        label: '分类名称',
        prop: 'cat_name'
      },
      {
        label: '是否有效',
        type: 'template',
        template: 'isok'
      },
      {
        label: '排序',
        type: 'template',
        template: 'order'
      },
      {
        label: '操作',
        type: 'template',
        template: 'opt'
      }
      ]
    }
  },
  methods: {
    // 弹出框的方法-----------
    setIsShow () {
      this.isShow = !this.isShow
      this.selectedKeys = []
      this.addCateFrom.cat_pid = 0
      this.addCateFrom.cat_level = 0
    },
    showPopUpBox (name) {
      this.getParentCateList()
      this.isShow = true
      this.tielename = name
    },
    // 弹出框的方法-----------

    // 获取商品的分类信息
    async getcareList () {
      const { data: res } = await instance.get('categories', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品分类失败')
      }
      // console.log(res.data)
      this.careList = res.data.result
      this.total = res.data.total
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getcareList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getcareList()
    },
    // 获取父级分类的数据
    async getParentCateList () {
      const { data: res } = await instance.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) {
        return this.$message.error('获取分级分类数据失败')
      } else {
        this.parentList = res.data
      }
      // console.log('------->', res.data)
    },
    parentCateChange () {
      // console.log(this.selectedKeys)
      if (this.selectedKeys.length > 0) {
        this.addCateFrom.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        this.addCateFrom.cat_level = this.selectedKeys.length
      } else {
        this.addCateFrom.cat_pid = 0
        this.addCateFrom.cat_level = 0
      }
    },
    async addCase () {
      // 添加分类
      const { data: res } = await instance.post('categories', this.addCateFrom)
      if (res.meta.status !== 201) {
        return this.$message.error('添加分类失败')
      } else {
        this.getcareList()
        this.setIsShow()
        return this.$message.success('添加分类成功')
      }
    }
  }
}
</script>

<style scoped lang="less">
  .treeBox{
    margin-top: 15px;
  }
</style>
